package com.supercube.sport.yoga.service.impl;

import com.github.pagehelper.PageHelper;
import com.supercube.common.utils.ShiroUtils;
import com.supercube.common.utils.StringUtils;
import com.supercube.common.utils.TimeUtils;
import com.supercube.sport.yoga.dao.CardSuperSettingsMapper;
import com.supercube.sport.yoga.dao.CardSupportVenuesMapper;
import com.supercube.sport.yoga.dao.CardTransferSettingsMapper;
import com.supercube.sport.yoga.dao.CoursesSupportedCardMapper;
import com.supercube.sport.yoga.dao.MemberCardBasicInfoMapper;
import com.supercube.sport.yoga.dao.MemberCardBindingInfoMapper;
import com.supercube.sport.yoga.dao.MemberConsumeRecordMapper;
import com.supercube.sport.yoga.dao.MemberIntegralRecordMapper;
import com.supercube.sport.yoga.dao.SettingPriceGradientMapper;
import com.supercube.sport.yoga.domain.*;
import com.supercube.sport.yoga.service.MemberCardService;
import com.supercube.sport.yoga.vo.BindCardBo;
import com.supercube.sport.yoga.vo.BindCardVo;
import com.supercube.sport.yoga.vo.CoursesSupportedCardVO;
import com.supercube.sport.yoga.vo.MemberCardBasicInfoBo;
import com.supercube.sport.yoga.vo.MemberCardBasicInfoVO;
import com.supercube.sport.yoga.vo.MemberCardBindingInfoVO;
import com.supercube.sport.yoga.vo.MemberCardDeductMoneyPo;
import com.supercube.sport.yoga.vo.MemberCardInfoVO;
import com.supercube.sport.yoga.vo.MemberCardStopPo;
import com.supercube.sport.yoga.vo.MemberCardVo;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
@Transactional
public class MemberCardServiceImpl implements MemberCardService {

    @Autowired
    private MemberCardBasicInfoMapper memberCardBasicInfoMapper;

    @Autowired
    private CardSupportVenuesMapper cardSupportVenuesMapper;

    @Autowired
    private CoursesSupportedCardMapper coursesSupportedCardMapper;

    @Autowired
    private CardTransferSettingsMapper cardTransferSettingsMapper;

    @Autowired
    private SettingPriceGradientMapper settingPriceGradientMapper;

    @Autowired
    private CardSuperSettingsMapper cardSuperSettingsMapper;

    @Autowired
    private MemberCardBindingInfoMapper bindingCardInfoMapper;

    @Autowired
    private MemberConsumeRecordMapper consumeRecordMapper;
    @Autowired
    private MemberIntegralRecordMapper integralRecordMapper;

    @Override
    public void save(MemberCardBasicInfoDTO dto) {
        // 会员卡基本信息
        memberCardBasicInfoMapper.insert(dto);
        // 支持场馆
        if (CollectionUtils.isNotEmpty(dto.getCardSupportVenuesDTOS())) {
            cardSupportVenuesMapper.batchInsert(dto.getCardSupportVenuesDTOS());
        }
        // 支持课程
        if (CollectionUtils.isNotEmpty(dto.getCoursesSupportedCardDTOS())) {
            coursesSupportedCardMapper.batchInsert(dto.getCoursesSupportedCardDTOS());
        }
        // 转让设置
        if (dto.getCardTransferSettingsDTO() != null) {
            cardTransferSettingsMapper.insert(dto.getCardTransferSettingsDTO());
        }
        // 价格设置
        if (CollectionUtils.isNotEmpty(dto.getSettingPriceGradientDTOS())) {
            settingPriceGradientMapper.batchInsert(dto.getSettingPriceGradientDTOS());
        }
        // 高级设置
        if (dto.getCardSuperSettingsDTO() != null) {
            cardSuperSettingsMapper.insert(dto.getCardSuperSettingsDTO());
        }
    }

    @Override
    public void update(MemberCardBasicInfoDTO dto) {
        memberCardBasicInfoMapper.updateByPrimaryKey(dto);
        String cardId = dto.getCardId();

        if (StringUtils.isNotEmpty(cardId)) {
            cardSupportVenuesMapper.deleteCardSupportVenuesByCardId(cardId);
        }
        if (CollectionUtils.isNotEmpty(dto.getCardSupportVenuesDTOS())) {
            cardSupportVenuesMapper.batchInsert(dto.getCardSupportVenuesDTOS());
        }

        // 修改支持的课程
        updateCoursesSupportedCard(dto.getCoursesSupportedCardDTOS(), cardId);

        cardTransferSettingsMapper.deleteCardTransferSettingsByCardId(cardId);
        if (dto.getCardTransferSettingsDTO() != null) {
            cardTransferSettingsMapper.insert(dto.getCardTransferSettingsDTO());
        }

        settingPriceGradientMapper.deleteSettingPriceGradientByCardId(cardId);
        if (CollectionUtils.isNotEmpty(dto.getSettingPriceGradientDTOS())) {
            settingPriceGradientMapper.batchInsert(dto.getSettingPriceGradientDTOS());
        }

        cardSuperSettingsMapper.deleteCardSuperSettingsByCardId(cardId);
        if (dto.getCardSuperSettingsDTO() != null) {
            cardSuperSettingsMapper.insert(dto.getCardSuperSettingsDTO());
        }
    }

    public void updateCoursesSupportedCard(List<CoursesSupportedCardDTO> importCSCList, String cardId) {
        // 数据库中已经存在的
        List<CoursesSupportedCardDTO> existCSCList = coursesSupportedCardMapper.selectCoursesSupportedCardByCardId(cardId);
        Set<String> existCSCCourseIdSet = existCSCList.stream().map(CoursesSupportedCardDTO::getCourseId).collect(Collectors.toSet());

        // 修改时页面传入的
        importCSCList = importCSCList.stream().collect(
                Collectors.collectingAndThen(
                        Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(CoursesSupportedCardDTO::getCourseId))), ArrayList::new));
        List<CoursesSupportedCardDTO> insertCoursesSupportedCardList = new ArrayList<>();
        List<CoursesSupportedCardDTO> updateCoursesSupportedCardList = new ArrayList<>();
        List<String> deleteCoursesSupportedCardList = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(importCSCList)) {
            importCSCList.forEach(csc -> {
                if (existCSCCourseIdSet.contains(csc.getCourseId())) {
                    updateCoursesSupportedCardList.add(csc);
                    existCSCCourseIdSet.remove(csc.getCourseId());
                } else {
                    csc.setCardId(cardId);
                    csc.setId(UUID.randomUUID().toString());
                    insertCoursesSupportedCardList.add(csc);
                }
            });
            deleteCoursesSupportedCardList.addAll(existCSCCourseIdSet);
        }
        if (CollectionUtils.isNotEmpty(updateCoursesSupportedCardList)) {
            coursesSupportedCardMapper.batchUpdateByCourseId(updateCoursesSupportedCardList);
        }
        if (CollectionUtils.isNotEmpty(insertCoursesSupportedCardList)) {
            coursesSupportedCardMapper.batchInsert(insertCoursesSupportedCardList);
        }
        if (CollectionUtils.isNotEmpty(deleteCoursesSupportedCardList)) {
            coursesSupportedCardMapper.batchDeleteByCourseId(deleteCoursesSupportedCardList);
        }
    }

    @Override
    public void deleteMemberCardInfoByCardId(String cardId) {
        cardSupportVenuesMapper.deleteCardSupportVenuesByCardId(cardId);
        coursesSupportedCardMapper.deleteCoursesSupportedCardByCardId(cardId);
        cardTransferSettingsMapper.deleteCardTransferSettingsByCardId(cardId);
        settingPriceGradientMapper.deleteSettingPriceGradientByCardId(cardId);
        cardSuperSettingsMapper.deleteCardSuperSettingsByCardId(cardId);
        memberCardBasicInfoMapper.deleteByPrimaryKey(cardId);
    }

    @Override
    public MemberCardInfoVO queryMemberCardInfoByCardId(String cardId) {
        MemberCardBasicInfoVO memberCardBasicInfoDTO = memberCardBasicInfoMapper.selectByPrimaryKey(cardId);
        if (memberCardBasicInfoDTO == null) {
            memberCardBasicInfoDTO = new MemberCardBasicInfoVO();
        }
        MemberCardInfoVO memberCardInfoVO = new MemberCardInfoVO(memberCardBasicInfoDTO);
        List<CardSupportVenuesDTO> venuesDTOS = cardSupportVenuesMapper.selectCardSupportVenuesByCardId(cardId);
        List<CoursesSupportedCardVO> coursesSupportedCardVOS = coursesSupportedCardMapper.selectCoursesSupportedCardByUserIdAndCardId(null/*ShiroUtils.getUserId()*/, cardId);
        CardTransferSettingsDTO cardTransferSettingsDTOS = cardTransferSettingsMapper.selectCardTransferSettingsByCardId(cardId);
        List<SettingPriceGradientDTO> settingPriceGradientDTOS = settingPriceGradientMapper.selectSettingPriceGradientByCardId(cardId);
        CardSuperSettingsDTO cardSuperSettingsDTOS = cardSuperSettingsMapper.selectCardSuperSettingsByCardId(cardId);
        if (CollectionUtils.isNotEmpty(venuesDTOS)) {
            memberCardInfoVO.setCardSupportVenuesDTOS(venuesDTOS.stream().map(CardSupportVenuesDTO::getVenuesId).collect(Collectors.toList()));
        }
        // 1:团课;2:小班课;3:私教课
        memberCardInfoVO.setManyClassCoursesSupportedCardVOS(coursesSupportedCardVOS.stream().filter(courses -> "1".equals(courses.getType())).collect(Collectors.toList()));
        memberCardInfoVO.setSmallClassCoursesSupportedCardVOS(coursesSupportedCardVOS.stream().filter(courses -> "2".equals(courses.getType())).collect(Collectors.toList()));
        memberCardInfoVO.setPrivateClassCoursesSupportedCardVOS(coursesSupportedCardVOS.stream().filter(courses -> "3".equals(courses.getType())).collect(Collectors.toList()));
        memberCardInfoVO.setCardTransferSettingsDTO(cardTransferSettingsDTOS);
        memberCardInfoVO.setSettingPriceGradientDTOS(settingPriceGradientDTOS);
        memberCardInfoVO.setCardSuperSettingsDTO(cardSuperSettingsDTOS);
        return memberCardInfoVO;
    }

    @Override
    public List<MemberCardBasicInfoVO> selectMemberCardInfoList(MemberCardBasicInfoBo dto) {
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        return memberCardBasicInfoMapper.selectMemberCardInfoList(dto);
    }

    @Override
    public int queryCountCardSupportVenuesByVenuesId(String venuesId) {
        return cardSupportVenuesMapper.selectCountCardSupportVenuesByVenuesId(venuesId);
    }

    public List<MemberCardVo> queryAllMemberCard(String cardId) {
        Long userId = ShiroUtils.getUserId();
        List<MemberCardVo> memberCardVos = memberCardBasicInfoMapper.selectAllMemberCardByCardIdAndUserId(cardId, userId);
        memberCardVos.forEach(memberCardVo -> {
            memberCardVo.setSettingPriceGradientDTOList(settingPriceGradientMapper.selectSettingPriceGradientByCardId(memberCardVo.getCardId()));
            memberCardVo.setCardSuperSettingsDTO(cardSuperSettingsMapper.selectCardSuperSettingsByCardId(memberCardVo.getCardId()));
        });
        return memberCardVos;
    }

    public List<BindCardVo> queryBindCardList(BindCardBo bo) {
        PageHelper.startPage(bo.getPageNum(), bo.getPageSize());
        return bindingCardInfoMapper.queryBindCardList(bo);
    }

    /**
     * 绑卡
     *
     * @param dto
     */
    @Override
    public void memberBindCard(MemberConsumeRecordDTO dto) {
        //插入绑卡表
        MemberCardBindingInfoDTO bind = new MemberCardBindingInfoDTO();
        ///*储值类型,1
        //次卡类型,2
        //期限类型,3*/
        if (!Objects.equals(dto.getCardType(), "3")) {
            dto.setCardOpeningTime(TimeUtils.getCurrentTime());
        }
        BeanUtils.copyProperties(dto, bind);
        Long userId = ShiroUtils.getUserId();
        bind.setStatus("1");//已绑定
        String bindId = UUID.randomUUID().toString();
        bind.setId(bindId);
        bind.setCreateUser(userId.toString());
        bind.setCreateTime(TimeUtils.getCurrentTime());
        bind.setTotalMoney(dto.getPaidInAmount());//累计金额
        bindingCardInfoMapper.insert(bind);
        //插入消费记录表
        dto.setId(UUID.randomUUID().toString());
        dto.setBindingCardId(bindId);
        dto.setConsumeStatement("会员绑卡");
        dto.setConsumeTime(TimeUtils.getCurrentTime());
        dto.setCreateUser(userId.toString());
        dto.setCardConsumeType("1");
        consumeRecordMapper.insert(dto);
        //插入积分表
        MemberIntegralRecordDTO integral = new MemberIntegralRecordDTO();
        integral.setId(UUID.randomUUID().toString());
        integral.setMemberId(dto.getMemberId());
        integral.setIntegralNumber(dto.getPaidInAmount());//积分
        integral.setIntegralBalance(dto.getPaidInAmount());//余额
        integral.setOperatorUser(userId.toString());
        integral.setOperatorTime(TimeUtils.getCurrentTime());
        integral.setReason("会员绑卡");
        integralRecordMapper.insert(integral);

    }

    /**
     * 续卡保存
     *
     * @param dto
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void memberRenewCard(MemberConsumeRecordDTO dto) {
        //查询绑卡信息
        MemberCardBindingInfoDTO bind = bindingCardInfoMapper.selectByPrimaryKey(dto.getBindingCardId());
        Long userId = ShiroUtils.getUserId();
        if (Objects.equals(dto.getCardType(), "1")) {
            //剩余金额
            bind.setStoredValueAmount(new BigDecimal(bind.getStoredValueAmount())
                    .add(new BigDecimal(dto.getStoredValueAmount())).toString());
        } else if (Objects.equals(dto.getCardType(), "2")) {
            //剩余私教次数（次卡）
            bind.setNumberPrivateTeaching(Integer.valueOf(bind.getNumberPrivateTeaching())
                    + Integer.valueOf(dto.getNumberPrivateTeaching()) + "");
            //剩余团课次数（次卡）
            bind.setNumberGroupLessons(Integer.valueOf(bind.getNumberGroupLessons())
                    + Integer.valueOf(dto.getNumberGroupLessons()) + "");
            //剩余小班次数（次卡）
            bind.setNumberSmallClasses(Integer.valueOf(bind.getNumberSmallClasses())
                    + Integer.valueOf(dto.getNumberSmallClasses()) + "");
        } else if (Objects.equals(dto.getCardType(), "3")) {
            //剩余期限天数
            bind.setDurationDays(Integer.valueOf(bind.getDurationDays())
                    + Integer.valueOf(dto.getDurationDays()) + "");
        }
        bind.setLastModifyUser(userId.toString());
        bind.setLastModifyTime(TimeUtils.getCurrentTime());
        bind.setVenueId(dto.getVenueId());
        //累计金额
        bind.setTotalMoney(new BigDecimal(dto.getPaidInAmount()).
                add(new BigDecimal(bind.getTotalMoney())).toString());
        bindingCardInfoMapper.updateByPrimaryKey(bind);
        //插入消费记录表
        dto.setBindingCardId(bind.getId());
        dto.setId(UUID.randomUUID().toString());
        dto.setConsumeStatement("会员续卡");
        dto.setConsumeTime(TimeUtils.getCurrentTime());
        dto.setCreateUser(userId.toString());
        dto.setCardConsumeType("2");
        consumeRecordMapper.insert(dto);
        //查询最新的积分表
        List<MemberIntegralRecordDTO> list = integralRecordMapper.getMemberIntegral(dto.getMemberId());
        MemberIntegralRecordDTO integralOld = list.get(0);
        MemberIntegralRecordDTO integral = new MemberIntegralRecordDTO();
        integral.setId(UUID.randomUUID().toString());
        integral.setMemberId(dto.getMemberId());
        //积分 = 充值金额
        integral.setIntegralNumber(dto.getPaidInAmount());//积分
        //余额 当前充值金额加上上次最新的消费余额。
        integral.setIntegralBalance(new BigDecimal(dto.getPaidInAmount()).
                add(new BigDecimal(integralOld.getIntegralBalance())).toString());
        integral.setOperatorUser(userId.toString());
        integral.setOperatorTime(TimeUtils.getCurrentTime());
        integral.setReason("会员续卡");
        integralRecordMapper.insert(integral);

    }

    @Override
    public MemberCardBasicInfoVO queryMemberCardBaseInfoByCardId(String cardId) {
        return memberCardBasicInfoMapper.selectByPrimaryKey(cardId);
    }

    /**
     * 扣费保存
     *
     * @param po
     */
    @Override
    public void memberCardDeductMoney(MemberCardDeductMoneyPo po) {
        //查询绑卡信息
        MemberCardBindingInfoDTO bind = bindingCardInfoMapper.selectByPrimaryKey(po.getBindingCardId());
        Long userId = ShiroUtils.getUserId();
        //储值类型
        if (Objects.equals(po.getCardType(), "1")) {
            //剩余金额
            bind.setStoredValueAmount(new BigDecimal(bind.getStoredValueAmount())
                    .subtract(new BigDecimal(po.getStoredValueAmount())).toString());
        } else if (Objects.equals(po.getCardType(), "2")) {
            //剩余私教次数（次卡）
            bind.setNumberPrivateTeaching(Integer.valueOf(bind.getNumberPrivateTeaching())
                    - Integer.valueOf(po.getNumberPrivateTeaching()) + "");
            //剩余团课次数（次卡）
            bind.setNumberGroupLessons(Integer.valueOf(bind.getNumberGroupLessons())
                    - Integer.valueOf(po.getNumberGroupLessons()) + "");
            //剩余小班次数（次卡）
            bind.setNumberSmallClasses(Integer.valueOf(bind.getNumberSmallClasses())
                    - Integer.valueOf(po.getNumberSmallClasses()) + "");
        } else {
            //期限类型
            //剩余期限天数
            bind.setDurationDays(Integer.valueOf(bind.getDurationDays())
                    - Integer.valueOf(po.getDurationDays()) + "");
        }
        bind.setLastModifyUser(userId.toString());
        bind.setLastModifyTime(TimeUtils.getCurrentTime());
        bindingCardInfoMapper.updateByPrimaryKey(bind);
        List<MemberConsumeRecordDTO> recordList = consumeRecordMapper.selectRecordDTOByMemberId(po.getMemberId(), po.getBindingCardId());
        MemberConsumeRecordDTO dto = recordList.get(0);
        //插入消费记录表
        dto.setBindingCardId(bind.getId());
        dto.setId(UUID.randomUUID().toString());
        dto.setConsumeStatement("扣费");
        dto.setConsumeTime(TimeUtils.getCurrentTime());
        dto.setCreateUser(userId.toString());
        dto.setCardConsumeType("5");
        //储值类型
        if (Objects.equals(po.getCardType(), "1")) {
            //剩余金额
            dto.setStoredValueAmount((new BigDecimal(po.getStoredValueAmount())).toString());
        } else if (Objects.equals(po.getCardType(), "2")) {
            //剩余私教次数（次卡）
            dto.setNumberPrivateTeaching(Integer.valueOf(po.getNumberPrivateTeaching()) + "");
            //剩余团课次数（次卡）
            dto.setNumberGroupLessons(Integer.valueOf(po.getNumberGroupLessons()) + "");
            //剩余小班次数（次卡）
            dto.setNumberSmallClasses(Integer.valueOf(po.getNumberSmallClasses()) + "");
        } else {
            //期限类型
            //剩余期限天数
            dto.setDurationDays(Integer.valueOf(po.getDurationDays()) + "");
        }
        consumeRecordMapper.insert(dto);
    }

    /**
     * 停卡
     *
     * @param po
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void memberCardStop(MemberCardStopPo po) {
        //查询绑卡信息
        MemberCardBindingInfoDTO bind = bindingCardInfoMapper.selectByPrimaryKey(po.getBindingCardId());
        Long userId = ShiroUtils.getUserId();
        if (Objects.equals(po.getStopCardType(), "2")) {
            //永久停卡
            //累计金额减去退费
            bind.setTotalMoney(new BigDecimal(bind.getTotalMoney())
                    .subtract(new BigDecimal(po.getMoney())).toString());
            //剩余金额清零
            bind.setStoredValueAmount("0");
            //剩余私教次数（次卡）
            bind.setNumberPrivateTeaching("0");
            //剩余团课次数（次卡）
            bind.setNumberGroupLessons("0");
            //剩余小班次数（次卡）
            bind.setNumberSmallClasses("0");
            //剩余期限天数
            bind.setDurationDays("0");
            bind.setLastModifyUser(userId.toString());
            bind.setLastModifyTime(TimeUtils.getCurrentTime());
            bind.setStatus("2");//永久停卡
            bindingCardInfoMapper.updateByPrimaryKey(bind);
            //插入消费记录
            List<MemberConsumeRecordDTO> recordList = consumeRecordMapper.selectRecordDTOByMemberId(po.getMemberId(), po.getBindingCardId());
            MemberConsumeRecordDTO dto = recordList.get(0);
            //插入消费记录表
            dto.setBindingCardId(bind.getId());
            dto.setId(UUID.randomUUID().toString());
            dto.setConsumeStatement("永久停卡");
            dto.setConsumeTime(TimeUtils.getCurrentTime());
            dto.setCreateUser(userId.toString());
            dto.setCardConsumeType("4");
            //剩余金额清零
            dto.setStoredValueAmount("0");
            //剩余私教次数（次卡）
            dto.setNumberPrivateTeaching("0");
            //剩余团课次数（次卡）
            dto.setNumberGroupLessons("0");
            //剩余小班次数（次卡）
            dto.setNumberSmallClasses("0");
            //剩余期限天数
            dto.setDurationDays("0");
            //实收金额为 负数
            dto.setPaidInAmount("-" + po.getMoney());
            //支付方式
            dto.setPaymentMethod(po.getPaymentMethod());
            consumeRecordMapper.insert(dto);
        } else {
            //临时停卡
            //累计金额 加上 收款
            bind.setTotalMoney(new BigDecimal(bind.getTotalMoney())
                    .add(new BigDecimal(po.getMoney())).toString());
            //临时停卡开始时间
            bind.setStopCardStartTime(po.getStopCardStartTime());
            //临时停卡结束时间
            bind.setStopCardEndTime(po.getStopCardEndTime());
            bind.setLastModifyUser(userId.toString());
            bind.setLastModifyTime(TimeUtils.getCurrentTime());
            bind.setStatus("3");//临时停卡
            bindingCardInfoMapper.updateByPrimaryKey(bind);
            //插入消费记录
            List<MemberConsumeRecordDTO> recordList = consumeRecordMapper.selectRecordDTOByMemberId(po.getMemberId(), po.getBindingCardId());
            MemberConsumeRecordDTO dto = recordList.get(0);
            //插入消费记录表
            dto.setBindingCardId(bind.getId());
            dto.setId(UUID.randomUUID().toString());
            dto.setConsumeStatement("临时停卡");
            dto.setConsumeTime(TimeUtils.getCurrentTime());
            dto.setCreateUser(userId.toString());
            dto.setCardConsumeType("3");
            //剩余金额清零
            dto.setStoredValueAmount("0");
            //剩余私教次数（次卡）
            dto.setNumberPrivateTeaching("0");
            //剩余团课次数（次卡）
            dto.setNumberGroupLessons("0");
            //剩余小班次数（次卡）
            dto.setNumberSmallClasses("0");
            //剩余期限天数
            dto.setDurationDays("0");
            //实收金额
            dto.setPaidInAmount(po.getMoney());
            //支付方式
            dto.setPaymentMethod(po.getPaymentMethod());
            consumeRecordMapper.insert(dto);
        }

    }/**
     * 激活会员卡
     *
     * @param po
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void memberCardActivate(MemberCardStopPo po) {
        //查询绑卡信息
        MemberCardBindingInfoDTO bind = bindingCardInfoMapper.selectByPrimaryKey(po.getBindingCardId());
        Long userId = ShiroUtils.getUserId();
        //临时停卡 激活
        //累计金额 加上 收款
        bind.setTotalMoney(new BigDecimal(bind.getTotalMoney())
                .add(new BigDecimal(po.getMoney())).toString());
        //临时停卡开始时间
        bind.setStopCardStartTime(null);
        //临时停卡结束时间
        bind.setStopCardEndTime(null);
        bind.setLastModifyUser(userId.toString());
        bind.setLastModifyTime(TimeUtils.getCurrentTime());
        bind.setStatus("1");//已绑定
        bindingCardInfoMapper.updateByPrimaryKey(bind);
        //插入消费记录
        List<MemberConsumeRecordDTO> recordList = consumeRecordMapper.selectRecordDTOByMemberId(po.getMemberId(), po.getBindingCardId());
        MemberConsumeRecordDTO dto = recordList.get(0);
        //插入消费记录表
        dto.setBindingCardId(bind.getId());
        dto.setId(UUID.randomUUID().toString());
        dto.setConsumeStatement("临时停卡激活");
        dto.setConsumeTime(TimeUtils.getCurrentTime());
        dto.setCreateUser(userId.toString());
        dto.setCardConsumeType("6");//激活
        /*//剩余金额清零
        dto.setStoredValueAmount("0");
        //剩余私教次数（次卡）
        dto.setNumberPrivateTeaching("0");
        //剩余团课次数（次卡）
        dto.setNumberGroupLessons("0");
        //剩余小班次数（次卡）
        dto.setNumberSmallClasses("0");
        //剩余期限天数
        dto.setDurationDays("0");*/
        //实收金额
        dto.setPaidInAmount(po.getMoney());
        //支付方式
        dto.setPaymentMethod(po.getPaymentMethod());
        consumeRecordMapper.insert(dto);


    }

    @Override
    public List<MemberCardInfoVO> queryMemberCardInfoByCourseId(String courseId) {

        return memberCardBasicInfoMapper.selectMemberCardInfoByCourseId(courseId);
    }

    @Override
    public List<MemberCardBindingInfoVO> queryMemberBindCardInfoList(Map<String, Object> map) {
        return bindingCardInfoMapper.selectMemberBindCardInfoList(map);
    }
    @Override
    public MemberCardInfoDTO getMemberCardInfo(String memberId, String  cardId){
        MemberCardInfoDTO dto = bindingCardInfoMapper.getMemberCardInfo(memberId,cardId);
        dto.setSettingPriceGradientDTOList(settingPriceGradientMapper.selectSettingPriceGradientByCardId(dto.getCardId()));
        return dto;
    }


}
